package bravo.xc.com.xc_bravo_android.operation.message;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import bravo.xc.com.xc_bravo_android.R;
import bravo.xc.com.xc_bravo_android.adapter.messageListViewAdapter;
import bravo.xc.com.xc_bravo_android.base.BaseMVPActivity;
import bravo.xc.com.xc_bravo_android.beans.MessageBeans;
import bravo.xc.com.xc_bravo_android.beans.MessageResultBeans;
import bravo.xc.com.xc_bravo_android.operation.messagedetails.MessageDetailsActivity;
import bravo.xc.com.xc_bravo_android.operation.person_center.PersonalCenterActivity;
import butterknife.ButterKnife;
import butterknife.Unbinder;


public class MessageActivity extends BaseMVPActivity<MessageContract.MessageView,MessageContract.MessagePresenter> implements View.OnClickListener,MessageContract.MessageView{

    private Context context=this;
    private LinearLayout layout_title_left;
    private TextView tv_title_meg;
    private ListView listView_message;
    private messageListViewAdapter messageListViewAdapter;
    private MessageBeans messageBeans;
    private MessageResultBeans messageResultBeans;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_title_left:
                Intent intent = new Intent(this, PersonalCenterActivity.class);
                startActivity(intent);
                finish();
                break;

        }
    }

    @Override
    public void showToast(String str) {
        Toast.makeText(MessageActivity.this,str,Toast.LENGTH_SHORT).show();
    }

    @Override
    protected MessageContract.MessagePresenter createPresnter() {
        return new MessagePresenter();
    }

    @Override
    protected void initView() {
        layout_title_left=(LinearLayout) findViewById(R.id.layout_title_left);
        tv_title_meg=(TextView)findViewById(R.id.tv_title_msg);
        tv_title_meg.setText("消息管理");

        listView_message=(ListView)findViewById(R.id.lv_message);

        layout_title_left.setOnClickListener(this);

        listView_message.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(context,MessageDetailsActivity.class);
                intent.putExtra("orderNum",messageListViewAdapter.getItem(position).getOrderNum());
                startActivity(intent);
            }
        });

    }

    @Override
    protected void initData() {
        messageResultBeans = new MessageResultBeans();
        messageBeans = new MessageBeans();
        messageBeans.setPageNum("");
        messageBeans.setUserId(String.valueOf(getSharedPreferences("UserInfo",MODE_PRIVATE).getInt("userId",0)));
        presenter.doGetMessage(messageBeans);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_message;
    }

    @Override
    protected Unbinder getBinder() {
        return ButterKnife.bind(MessageActivity.this);
    }

    @Override
    protected void onChildDestroy() {
        presenter.onDetached();
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void setAdapter(MessageResultBeans messageResultBeans) {
        this.messageResultBeans = messageResultBeans;
        messageListViewAdapter =new messageListViewAdapter(context,R.layout.item_message, messageResultBeans.getData());
        listView_message.setAdapter(messageListViewAdapter);
    }
}
